package website.haicheng.server.handlers;

import cn.hutool.core.util.StrUtil;
import website.haicheng.server.common.annotations.Handler;
import website.haicheng.server.common.constant.AttrConstant;
import website.haicheng.server.common.enums.MessageEnum;
import website.haicheng.server.config.NettyManage;
import website.haicheng.server.model.Message;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: 582895699@qq.com
 * @Desctription: 下线逻辑
 * @Date: Created in 2021/12/1 16:29
 */
@Slf4j
@Component
@Handler(value = MessageEnum.CLOSE_CHANNEL)
public class CloseHandler implements MessageHandler {

    @Resource
    private NettyManage nettyManage;

    /**
     * 逻辑处理
     *
     * @param channel Channel
     * @param message Message
     */
    @Override
    public void process(Channel channel, Message message) {
        String account = channel.attr(AttrConstant.ATTRIBUTE_KEY_ACCOUNT).get();
        // 未认证用户离线，直接跳出
        if (StrUtil.isBlank(account)) {
            return;
        }
        // 从内存中删除
        nettyManage.remove(channel, account);
        // 从数据库中删除
        log.info("用户下线;account:[{}]", account);
    }
}
